package com.haochang.chunkvod.view.vod.title;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 *@author qintao
 *created at 2016/6/1 11:35
 */

public class TitleView extends RelativeLayout
{
	private ImageView img_back, img_operates;
	private LinearLayout backLayout;
	private BaseTextView tv_title;
	private BaseTextView tv_back;//返回按钮附带的文字
	private ITitleOnClickListener mTitleOnClickListener;

	public void setTitleOnClickListener(ITitleOnClickListener mTitleOnClickListener)
	{
		this.mTitleOnClickListener = mTitleOnClickListener;
	}

	public TitleView(Context context, AttributeSet attrs, int defStyleAttr)
	{
		super(context, attrs, defStyleAttr);
		initView(context);
	}

	public TitleView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView(context);
	}

	public TitleView(Context context)
	{
		super(context);
		initView(context);
	}

	private void initView(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.view_title, this, true);
		backLayout = (LinearLayout) view.findViewById(R.id.backLayout);
		img_back = (ImageView) view.findViewById(R.id.img_back);
		img_operates = (ImageView) view.findViewById(R.id.img_operates);
		tv_title = (BaseTextView) view.findViewById(R.id.tv_title);
		tv_back = (BaseTextView) view.findViewById(R.id.tv_back);
		backLayout.setOnClickListener(mOnBaseClickListener);
		img_operates.setOnClickListener(mOnBaseClickListener);
	}

	public TitleView setTitle(String title)
	{
		tv_title.setText(title);
		return this;
	}

	public TitleView setTitle(int resource)
	{
		tv_title.setText(resource);
		return this;
	}

	public TitleView setBackText(CharSequence charSequence)
	{
		return setBackText(charSequence, false);
	}

	/**
	 * 设置返回箭头右边的文字
     */
	public TitleView setBackText(CharSequence charSequence, boolean hideBackImageView)
	{
		if (!TextUtils.isEmpty(charSequence))
		{
			img_back.setImageResource(R.drawable.ic_back);
			tv_back.setVisibility(VISIBLE);
			tv_back.setText(charSequence);
		}
		if (hideBackImageView)
			img_back.setVisibility(GONE);
		return this;
	}

	public TitleView setBackText(int resource, boolean hideBackImageView)
	{
		img_back.setImageResource(R.drawable.ic_back);
		tv_back.setVisibility(VISIBLE);
		tv_back.setText(resource);
		if (hideBackImageView)
			img_back.setVisibility(GONE);
		return this;
	}

	/**
	 * 设置右边按钮是否显示
	 * @param flag
	 * @return
	 */
	public TitleView setRightIconVISIBLE(boolean flag)
	{
		img_operates.setVisibility(flag ? View.VISIBLE : View.INVISIBLE);
		return this;
	}

	/**
	 * 设置左边按钮是否显示
	 * @param flag
	 * @return
	 */
	public TitleView setLeftIconVISIBLE(boolean flag)
	{
		img_back.setVisibility(flag ? View.VISIBLE : View.INVISIBLE);
		return this;
	}

	public interface ITitleOnClickListener
	{
		void onBackClick();

		void onDoneClick();
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.backLayout:
				if (null != mTitleOnClickListener)
					mTitleOnClickListener.onBackClick();
				break;
			case R.id.img_operates:
				if (null != mTitleOnClickListener)
					mTitleOnClickListener.onDoneClick();
				break;
			default:
				break;
			}
		}
	};
}
